package org.karelun.rabbit.bridge.ui.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.actions.BaseSelectionListenerAction;
import org.karelun.rabbit.bridge.ITransactionEditingDomain;

public class RabbitCommandActionHandler extends BaseSelectionListenerAction{

	protected ITransactionEditingDomain editingDomain;
	
	protected Command command;
	
	public RabbitCommandActionHandler(ITransactionEditingDomain domain) {
		super("");
		this.editingDomain=domain;
	}
	
	public RabbitCommandActionHandler(ITransactionEditingDomain domain,String label){
		super(label);
		this.editingDomain=domain;
	}

	public ITransactionEditingDomain getEditingDomain() {
		return editingDomain;
	}
	
	public void setEditingDomain(ITransactionEditingDomain editingDomain) {
		this.editingDomain = editingDomain;
	}
	
	@Override
	public void run() {
		editingDomain.getCommandStack().execute(command);
	}
	
	@Override
	protected boolean updateSelection(IStructuredSelection selection) {
		List<?> list = selection.toList();
		Collection<Object> collection=new ArrayList<Object>(list);
		command=createCommand(collection);
		if(editingDomain.getCDOView().isReadOnly()){
			return false;
		}
		return command.canExecute();
	}

	protected Command createCommand(Collection<Object> collection) {
		return UnexecutableCommand.INSTANCE;
	}
}
